<template>
	<view class="fui-wrap">
		<fui-sticky>
			<view style="background-color: #fff;">
				<fui-segmented-control @click="changeSegmented" :current="current" color="#000" activeColor="#0a8cdd" type="text" :values="segmented"></fui-segmented-control>
			</view>
		</fui-sticky>
		<fui-list>
			<fui-list-cell v-for="(item,index) in list" arrow :key="index" @click="$navigateTo('index/coupondetail?id='+item.id)">
				<view class="line">
					<view>
						<fui-text :text="item.coupon.title" type="black" size="28" block></fui-text>
						<block v-if="item.records.length===0">
						<fui-text text="未入场" type="danger" size="24" block></fui-text>
						</block>
						<block v-if="item.records.length>0">
						<fui-text v-if="item.records[item.records.length-1].entry_time" :text="'入场：'+formatDateTime(item.records[item.records.length-1].entry_time)" type="success" size="24" block></fui-text>
						<fui-text v-if="item.records[item.records.length-1].exit_time" :text="'离场：'+formatDateTime(item.records[item.records.length-1].exit_time)" type="success" size="24" block></fui-text>
						</block>
					</view>
					<plateType :plate_number="item.plate_number" :plate_type="item.records.length>0?item.records[0].plate_type:''"></plateType>
				</view>
			</fui-list-cell>
		</fui-list>
		<fui-empty v-if="list.length===0" :src="`${baseUrl}assets/img/miniapp/empty.png`" title="暂无数据!" :marginTop="200"></fui-empty>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import {formatDateTime} from '@/utils/util.js';
	import plateType from '@/components/common/platetype.vue';
	export default {
		components:{
			plateType:plateType
		},
		data() {
			return {
				baseUrl:baseUrl,
				segmented:['未使用','已使用','使用中','已过期'],
				current:0,
				page:1,
				endData:false,
				list:[]
			}
		},
		onLoad(e) {
			this.getCouponList();
		},
		onPullDownRefresh() {
			this.list=[];
			this.page=1;
			this.endData=false;
			this.getCouponList(function(){
				uni.stopPullDownRefresh();
			});
		},
		onReachBottom() {
			this.getCouponList();
		},
		methods: {
			...methods,
			changeSegmented:function(e){
				this.current=e.index;
				this.list=[];
				this.page=1;
				this.endData=false;
				this.getCouponList();
			},
			getCouponList:function(callback=false){
				this.showPopup=false;
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get('index/coupon',{
					type:this.current,
					page:this.page
				},true).then(res=>{
					this.list=this.list.concat(res);
					this.page++;
					if(res.length<10){
						this.endData=true;
					}	
					if(callback){
						callback();
					}
				});
			},
			formatDateTime:function(e){
				let r=formatDateTime(new Date(e*1000));
				return r.slice(0,16);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-wrap{
		padding-top: 0rpx;
	}
	.fui-custom__wrap{
		margin-top: 30rpx;
		padding: 20rpx 48rpx;
		padding-top: 0;
	}
	.line{
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>